﻿using System.Collections.ObjectModel;
using Resn.WhuPub.BLL;
using Resn.WhuPub.DAL;
using Resn.WhuPub.DML;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Collections.ObjectModel;

namespace Resn.WhuPub.BLL
{
        /// <summary>
        /// 实现学科的属性和方法
        /// </summary>
        public class Subject :Entity<SubjectModel>
        {
                #region 构造函数

                /// <summary>
                /// 初始化学科新实例<see cref="Subject"/>.
                /// </summary>
                /// <param name="model">学科数据模型</param>
                public Subject(SubjectModel model)
                        : base(model)
                {
                }

                #endregion 构造函数

                #region 属性

                #region 基本属性

               

                /// <summary>
                /// 获取学科所属门类标识
                /// </summary>
                /// <value>
                /// 学科所属门类标识
                /// </value>
                public string CategoryId
                {
                        get
                        {
                                return Model.CategoryId;
                        }
                }

                /// <summary>
                /// 获取或设置学科的名称
                /// </summary>
                /// <value>
                /// 学科的名称
                /// </value>
                public string Name
                {
                        get
                        {
                                return Model.Name;
                        }
                        set
                        {
                                Contract.Requires<ArgumentOutOfRangeException>(string.IsNullOrEmpty(value));

                                Model.Name = value;
                        }
                }

                /// <summary>
                /// 获取或设置学科的代码
                /// </summary>
                /// <value>
                /// 学科的代码
                /// </value>
                public string Code
                {
                        get
                        {
                                return Model.Code;
                        }
                        set
                        {
                                Contract.Requires<ArgumentOutOfRangeException>(string.IsNullOrEmpty(value));

                                Model.Code = value;
                        }
                }

                #endregion 基本属性

                

                /// <summary>
                /// 获取学科所属门类
                /// </summary>
                /// <value>
                /// 学科所属门类
                /// </value>
                public Category Category
                {
                        get
                        {
                                var model = DBStorage.Instance.Select<CategoryModel>(CategoryId);
                                return new Category(model);
                        }
                }

                /// <summary>
                /// 获取学科下属的专业
                /// </summary>
                /// <value>
                /// 学科下属的专业
                /// </value>
                public ObservableCollection<Major> Majors
                {
                        get
                        {
                                var models = DBStorage.Instance.Select<MajorModel>().Where(i => i.SubjectId == Id).ToList();

                                var result = new ObservableCollection<Major>();

                                models.ForEach(i => result.Add(new Major(i)));

                                return result;
                        }
                }

                #endregion 属性

                #region 方法

                /// <summary>
                /// 获取指定标识的对象
                /// </summary>
                /// <param name="id">标识</param>
                /// <returns></returns>
                public static Subject Select(string id)
                {
                        return Select<Subject>(id);
                }

                /// <summary>
                /// 创建专业
                /// </summary>
                /// <param name="name">名称</param>
                /// <param name="code">代码</param>
                /// <returns>
                /// 创建的专业对象
                /// </returns>
                /// <exception cref="System.NotImplementedException"></exception>
                public Major CreateMajor(string name, string code)
                {
                        var model = new MajorModel
                        {
                                Id = Guid.NewGuid().ToString(),
                                SubjectId = Id,
                                Name = name,
                                Code = code
                        };

                        return new Major(model);
                }



                
                #endregion 方法
        }
}